﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Xeml.Sampling;
using System.Data.Odbc;
using MySql.Data.MySqlClient;

namespace SQLDataProviders
{
    [Serializable()]
    [DefaultProperty("Server")]
    public class MySQLDataProviderConfig: INotifyPropertyChanged
    {
        #region const
        const string DefaultServer = "hanna.mpimp-golm.mpg.de";
        const string DefaultDataBase = "xeml_testing";
        const string DefaultUser = "mssql";
        const string DefaultPwd = "mssql";
        #endregion const

        #region constructor
        public MySQLDataProviderConfig()
        {
            #if DEBUG
            Server = DefaultServer;
            DataBase = DefaultDataBase;
            User = DefaultUser;
            Password = DefaultPwd;
            #endif
        }
        #endregion constructor

        private string _User;
        [Category("username")
                , Description("The login name")
                , ReadOnly(false)
                , DisplayName("username")
                , DefaultValue(DefaultUser)]
        public string User
        {
            get { return _User; }
            set
            {
                if (value != _User) //check whether the new value differs from the old value 
                {
                    _User = value; //set the value to member variable 
                    OnPropertyChanged("User"); //fire event to inform possible subscriber about the changed property 
                }
            }
        }

        private string _Password;
        [Category("password")
                , Description("The password for the user to login into the server where the data are located")
                , ReadOnly(false)
                , DisplayName("password")
                , DefaultValue(DefaultPwd)]
        public string Password
        {
            get { return _Password; }
            set
            {
                if (value != _Password) //check whether the new value differs from the old value 
                {
                    _Password = value; //set the value to member variable 
                    OnPropertyChanged("Password"); //fire event to inform possible subscriber about the changed property 
                }
            }
        }

        public string ConnectionString {
            get {
                MySqlConnectionStringBuilder sb = new MySqlConnectionStringBuilder();
                sb.UserID = User;
                sb.Password = Password;
                sb.Server = Server;
                sb.Database = DataBase;

                return sb.ConnectionString;
            }
        }

        private string _Server;
        [Category("database connection")
                , Description("The name of the server where the data are located.")
                , ReadOnly(false)
                , DisplayName("server name")
                , DefaultValue(DefaultServer)]
        public string Server
        {
            get { return _Server; }
            set
            {
                if (value != _Server) //check whether the new value differs from the old value 
                {
                    _Server = value; //set the value to member variable 
                    OnPropertyChanged("Server"); //fire event to inform possible subscriber about the changed property 
                }
            }
        }

        private string _DataBase;
        [Category("database connection")
                , Description("The name of the database.")
                , ReadOnly(false)
                , DisplayName("database name")
                , DefaultValue(DefaultDataBase)]
        public string DataBase
        {
            get { return _DataBase; }
            set
            {
                if (value != _DataBase) //check whether the new value differs from the old value 
                {
                    _DataBase = value; //set the value to member variable 
                    OnPropertyChanged("DataBase"); //fire event to inform possible subscriber about the changed property 
                }
            }
        }

        //private string _Driver;
        //[Category("database connection")
        //        , Description("The used MySQL driver")
        //        , ReadOnly(true)
        //        , DisplayName("driver name")
        //        , DefaultValue(DefaultDriver)]
        //public string Driver
        //{
        //    get { return _Driver; }
        //    set
        //    {
        //        if (value != _Driver)
        //        {
        //            _Driver = value;
        //            OnPropertyChanged("Driver");
        //        }
        //    }
        //}

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// fires the property changed event of an given property name
        /// </summary>
        protected virtual void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        #endregion INotifyPropertyChanged
    }
}